<?php
/* 用户授信表 */
namespace app\msg\model;


use MongoDB\Driver\Command;
use MongoDB\Driver\Manager;
use think\Db;
use think\Model;

class CustomerMapping extends  Model
{

    protected $tableName = 'col';

    /**
     * 获取一条记录
     * @param  array     $conditions
     * @return array
     */
    public function getOne($conditions)
    {
        if(empty($conditions)){
            return false;
        }
        $result = Db::connect('db_mongo')->name($this->tableName)->where($conditions)->find();
        return $result;
    }


    /**
     * 获取多条记录
     * @param array $conditions
     * @return array
     */
    public function getAll($conditions)
    {
        if(empty($conditions)){
            return false;
        }
        $result = Db::connect('db_mongo')->name($this->tableName)->where($conditions)->select();
        return $result;
    }


    /**
     * 数据库插入数据
     * @param array $data
     * @return int|string
     */
    public function insertData($data)
    {
        if(empty($data)){
            return false;
        }
        $result = Db::connect('db_mongo')->name($this->tableName)->insert($data);
        return $result;
    }


    /**
     * @param array $conditions
     * @param array $data
     * @return int|string
     */
    public function modifyData($conditions, $data)
    {
        if(!empty($conditions || empty($data))){
            return false;
        }
        $result = Db::connect('db_mongo')->name($this->tableName)->where($conditions)->update($data);
        return $result;
    }


    /**
     * 获取自增ID
     * @param string $name
     * @return int
     */

    public  function genAutoIncrementId($name = ''){
        if(!$name){
            $name = 'customerid';
        }
        $update = array('$inc'=>array("id"=>1));
        $query = array('name'=>$name);
        $command = array(
            'findandmodify'=> 'customerids',
            'update'       => $update,
            'query'        => $query,
            'new'          => true,
            'upsert'       => true
        );

        $instance = new  Command($command);
        $id = Db::connect('db_mongo')->command( $instance, $dbName = '', $readPreference = null, $class = false, $typeMap = null);

        return (int)$id[0]['value']['id'];
    }



}